﻿using System;
using System.Collections.Generic;
using System.Linq;
using VideoSeller.Business.Data;

namespace VideoSeller.Business.Models
{
	public class TeamChatModel : Repository<TeamChat>
	{
		public IEnumerable<TeamChat> GetChat(int onlineContentId)
		{
			return Table.Where(x => x.OnlineContentId == onlineContentId).OrderByDescending(x => x.CreateTime);
		}

		public void AddNewMessage(int onlineContentId, Guid userId, string message)
		{
			if (!string.IsNullOrEmpty(message))
			{
				var chat = new TeamChat
					{
						UserId = userId,
						OnlineContentId = onlineContentId,
						Message = message,
						CreateTime = DateTime.Now
					};
				Insert(chat);
			}
		}

		public void DeleteAll(IEnumerable<TeamChat> items)
		{
			foreach (var item in items)
			{
				var oncat = Table.SingleOrDefault(x => x.Id == item.Id);

				if (oncat != null)
				{
					Table.DeleteOnSubmit(oncat);
				}
			}

			SubmitAllChanges();
		}
	}
}